package oa.hleast.xswl.controller.admin.pms;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.common.result.ResultCode;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.pojo.bo.admin.AdminProductBO;
import oa.hleast.xswl.pojo.domain.pms.PmsSpu;
import oa.hleast.xswl.service.pms.IPmsSpuService;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "【系统管理】商品信息")
@RestController
@RequestMapping("/api.admin/v1/products")
@Slf4j
@AllArgsConstructor
public class SpuController {

    private IPmsSpuService iPmsSpuService;

    @ApiOperation(value = "商家商品列表分页(已提交审核的)", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum", dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "商品名称", paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "categoryId", value = "商品类目", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "status", value = "上架or下架", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "reviewStatus", value = "审核状态 0待审核 10审核通过 11审核驳回", paramType = "query", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping("/findPmsSpuApplyForForClerk")
    public Result<List<PmsSpu>> findPmsSpuApplyForForClerk(String queryMode, Integer page, Integer limit, String name, Long categoryId, Integer status, Integer reviewStatus) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            case PAGE:
                IPage<PmsSpu> result = iPmsSpuService.pagePmsSpuForClerk(page, limit, name, categoryId, status, reviewStatus, 1);
                iPmsSpuService.list(
                        new Page<>(page, limit),
                        new PmsSpu().setName(name).setCategoryId(categoryId)
                );
                return Result.success(result.getRecords(), result.getTotal());
            default:
                return Result.failed(ResultCode.QUERY_MODE_IS_NULL);
        }
    }

    @ApiOperation(value = "商家商品列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum", dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "商品名称", paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "categoryId", value = "商品类目", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "status", value = "上架or下架", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "reviewStatus", value = "审核状态 0待审核 10审核通过 11审核驳回", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "applyFor", value = "是否已提交申请 0未提交 1已提交", paramType = "query", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping("/findPmsSpuForClerk")
    public Result findPmsSpuForClerk(String queryMode, Integer page, Integer limit, String name, Long categoryId, Integer status, Integer reviewStatus, Integer applyFor) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            case PAGE:

                IPage<PmsSpu> result = iPmsSpuService.pagePmsSpuForClerk(page, limit, name, categoryId, status, reviewStatus, applyFor);

                return Result.success(result.getRecords(), result.getTotal());
            default:
                return Result.failed(ResultCode.QUERY_MODE_IS_NULL);
        }
    }

    @ApiOperation(value = "商家已上架的商品列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum", dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping("/findUpPmsSpuForClerk")
    public Result findUpPmsSpuForClerk(String queryMode, Integer page, Integer limit) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            case PAGE:
                IPage<PmsSpu> result = iPmsSpuService.pagePmsSpuForClerk(page, limit, null, null, 1, 10, null);

                return Result.success(result.getRecords(), result.getTotal());
            default:
                return Result.failed(ResultCode.QUERY_MODE_IS_NULL);
        }
    }

    @ApiOperation(value = "管理员列表分页(已提交审核的)", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum", dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "商品名称", paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "categoryId", value = "商品类目", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "reviewStatus", value = "审核状态", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "storeId", value = "店家id", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "status", value = "上架or下架", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "reviewStatus", value = "审核状态 0待审核 10审核通过 11审核驳回", paramType = "query", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping("/findPmsSpuForAdmin")
    public Result findPmsSpuForAdmin(String queryMode, Integer page, Integer limit, String name, Integer reviewStatus, Long storeId, Integer status, Long categoryId) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            case PAGE:
                IPage<PmsSpu> result = iPmsSpuService.pagePmsSpuForAdmin(page, limit, name, reviewStatus, storeId, status, 1, categoryId);

                return Result.success(result.getRecords(), result.getTotal());
            default:
                return Result.failed(ResultCode.QUERY_MODE_IS_NULL);
        }
    }

    @ApiOperation(value = "管理员列表分页(全部)", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum", dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "商品名称", paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "categoryId", value = "商品类目", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "reviewStatus", value = "审核状态", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "storeId", value = "店家id", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "status", value = "上架or下架", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "reviewStatus", value = "审核状态 0待审核 10审核通过 11审核驳回", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "applyFor", value = "是否已提交申请 0未提交 1已提交", paramType = "query", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping("/findPmsSpuForAdminAll")
    public Result findPmsSpuForAdminAll(String queryMode, Integer page, Integer limit, String name, Integer reviewStatus, Long storeId, Integer status, Integer applyFor, Long categoryId) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            case PAGE:
                IPage<PmsSpu> result = iPmsSpuService.pagePmsSpuForAdmin(page, limit, name, reviewStatus, storeId, status, applyFor, categoryId);

                return Result.success(result.getRecords(), result.getTotal());
            default:
                return Result.failed(ResultCode.QUERY_MODE_IS_NULL);
        }
    }

    @ApiOperation(value = "商品详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "商品id", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class)
    @GetMapping("/{id}")
    public Result detail(@PathVariable Long id) {
        AdminProductBO spu = iPmsSpuService.getBySpuId(id);
        return Result.success(spu);
    }


    @ApiOperation(value = "新增商品", httpMethod = "POST")
    @ApiImplicitParam(name = "spuBO", value = "实体JSON对象", required = true, paramType = "body", dataType = "AdminProductBO", dataTypeClass = AdminProductBO.class)
    @PostMapping
    public Result add(@RequestBody AdminProductBO spuBO) {
        boolean status = iPmsSpuService.add(spuBO);
        return Result.judge(status);
    }

    @ApiOperation(value = "修改商品", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "spu", value = "实体JSON对象", required = true, paramType = "body", dataType = "PmsSpu", dataTypeClass = PmsSpu.class)
    })
    @PostMapping(value = "/put/{id}")
    public Result update(
            @PathVariable Long id,
            @RequestBody AdminProductBO spu) {
        boolean status = iPmsSpuService.updateById(spu);
        return Result.judge(status);
    }

    @ApiOperation(value = "删除商品", httpMethod = "POST")
    @ApiImplicitParam(name = "ids", value = "id集合,以英文逗号','分隔", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class)
    @PostMapping("/delete/{ids}")
    public Result delete(@PathVariable String ids) {
        iPmsSpuService.removeBySpuIds(Arrays.asList(ids.split(",")).stream().map(id -> Long.parseLong(id)).collect(Collectors.toList()));
        return Result.success();
    }

    @ApiOperation(value = "修改商品", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "spu", value = "实体JSON对象", required = true, paramType = "body", dataType = "PmsSpu", dataTypeClass = PmsSpu.class)
    })
    @PostMapping(value = "/patch/{id}")
    public Result patch(@PathVariable Integer id, @RequestBody PmsSpu spu) {
        LambdaUpdateWrapper<PmsSpu> updateWrapper = new LambdaUpdateWrapper<PmsSpu>().eq(PmsSpu::getId, id);
        updateWrapper.set(spu.getStatus() != null, PmsSpu::getStatus, spu.getStatus());
        boolean update = iPmsSpuService.update(updateWrapper);
        return Result.success(update);
    }

    @ApiOperation(value = "申请审核", httpMethod = "GET")
    @GetMapping("/apply")
    public Result apply(@RequestParam Long id) {

        boolean status = iPmsSpuService.apply(id);

        return Result.judge(status);
    }

    @ApiOperation(value = "批量申请审核", httpMethod = "GET")
    @GetMapping("/applyList")
    public Result applyList(@RequestParam String ids) {

        boolean status = iPmsSpuService.applyList(ids);

        return Result.judge(status);
    }

    @ApiOperation(value = "审核通过", httpMethod = "GET")
    @GetMapping("/reviewPass")
    public Result reviewPass(@RequestParam Long id) {

        Long userId = RequestUtils.getUserId();
        boolean status = iPmsSpuService.reviewPass(id, userId);

        return Result.judge(status);
    }

    @ApiOperation(value = "批量审核通过", httpMethod = "GET")
    @GetMapping("/reviewPassList")
    public Result reviewPassList(@RequestParam String ids) {

        Long userId = RequestUtils.getUserId();
        boolean status = iPmsSpuService.reviewPassList(ids, userId);

        return Result.judge(status);
    }

    @ApiOperation(value = "审核驳回", httpMethod = "GET")
    @GetMapping("/reviewNotPass")
    public Result reviewNotPass(@RequestParam Long id, @RequestParam String remark) {

        Long userId = RequestUtils.getUserId();
        boolean result = iPmsSpuService.reviewNotPass(userId, id, remark);
        return Result.judge(result);
    }

    @ApiOperation(value = "商品上架", httpMethod = "GET")
    @GetMapping("/takeEffect")
    public Result takeEffect(@RequestParam Long id) {

        boolean result = iPmsSpuService.takeEffect(id);
        return Result.judge(result);
    }


    @ApiOperation(value = "批量商品上架", httpMethod = "GET")
    @GetMapping("/takeEffectList")
    public Result takeEffectList(@RequestParam String ids) {

        boolean result = iPmsSpuService.takeEffectList(ids);
        return Result.judge(result);
    }

    /**
     * 商品下架
     */
    @ApiOperation(value = "商品下架", httpMethod = "GET")
    @GetMapping("/invalidation")
    public Result invalidation(Long id) {
        boolean result = iPmsSpuService.invalidation(id);
        return Result.judge(result);
    }

    /**
     * 商品批量下架
     */
    @ApiOperation(value = "批量商品下架", httpMethod = "GET")
    @GetMapping("/invalidationList")
    public Result invalidationList(String ids) {
        List<String> spuIds = Arrays.asList(ids.split(","));

        boolean result = iPmsSpuService.invalidationList(ids);
        return Result.judge(result);
    }

    /**
     * 管理员批量下架商品
     */
    @ApiOperation(value = "管理员批量下架商品", httpMethod = "GET")
    @GetMapping("/adminInvalidationList")
    public Result adminInvalidationList(String ids) {

        boolean result = iPmsSpuService.invalidationList(ids);
        return Result.judge(result);
    }

}
